$(document).ready(function () {
    var $n = $("#number");
    if ($n.text() > 0) $n.css("color", "orange");
    else $n.css("color", "red");
    var hh = document.documentElement.clientHeight;
    var ls = document.documentElement.clientWidth;
    if (ls < 640) {
        $(".select dt").click(function () {
            if ($(this).next("div").css("display") == 'none') {
                $(".theme-popover-mask").height(hh);
                $(".theme-popover").css("position", "fixed");
                $(this).next("div").slideToggle("slow");
                $(".select div").not($(this).next()).hide();
            } else {
                $(".theme-popover-mask").height(0);
                $(".theme-popover").css("position", "static");
                $(this).next("div").slideUp("slow");
            }
        })


        $(".eliminateCriteria").on("click", function () {
            $(".dd-conent").hide();
        })

        $(".select dd").on("click", function () {
            $(".theme-popover-mask").height(0);
            $(".theme-popover").css("position", "static");
            $(".dd-conent").hide();
        });
        $(".theme-popover-mask").on("click", function () {
            $(".theme-popover-mask").height(0);
            $(".theme-popover").css("position", "static");
            $(".dd-conent").hide();
        });

    }


    $("span.love").click(function () {
        $(this).toggleClass("active");
    });

    var selectedClassify = ["A", "B", "C", "D", "F", "G", "H", "I", "J", "K"], pointer = 0;

    $(".select-list dd").click(function () {
        $(this).addClass("selected").siblings().removeClass("selected");
        if ($(this).hasClass("select-all")) {
            $("#selectA").remove();
        } else {
            var copyThisA = $(this).clone();
            if ($("#selectA").length > 0) {
                $("#selectA a").html($(this).text());
            } else {
                $(".select-result dl").append(copyThisA.attr("id", "selectA"));
                copyThisA.click(function () {
                    $(this).remove();
                    $("#select1 .select-all").addClass("selected").siblings().removeClass("selected");
                    hideResult();
                });
            }
        }
    });

    /*
    $("#select2 dd").click(function () {
        $(this).addClass("selected").siblings().removeClass("selected");
        if ($(this).hasClass("select-all")) {
            $("#selectB").remove();
        } else {
            var copyThisB = $(this).clone();
            if ($("#selectB").length > 0) {
                $("#selectB a").html($(this).text());
            } else {
                $(".select-result dl").append(copyThisB.attr("id", "selectB"));
                copyThisB.click(function () {
                    $(this).remove();
                    $("#select2 .select-all").addClass("selected").siblings().removeClass("selected");
                    hideResult();
                });
            }
        }
    });

    $("#select3 dd").click(function () {
        $(this).addClass("selected").siblings().removeClass("selected");
        if ($(this).hasClass("select-all")) {
            $("#selectC").remove();
        } else {
            var copyThisC = $(this).clone();
            if ($("#selectC").length > 0) {
                $("#selectC a").html($(this).text());
            } else {
                $(".select-result dl").append(copyThisC.attr("id", "selectC"));
                copyThisC.click(function () {
                    $(this).remove();
                    $("#select3 .select-all").addClass("selected").siblings().removeClass("selected");
                    hideResult();
                });
            }
        }
    });
    */

    function hideResult() {
        var dd = $(".select-result").find("dd");
        console.log(dd.length);
        if (dd.length <= 1)
            $(".select-result").hide();
    }

    $(".select dd").on("click", function () {
        if ($(".select-result dd").length > 1) {
            $(".select-no").hide();
            $(".eliminateCriteria").show();
            $(".select-result").show();
        } else {
            $(".select-no").show();
            $(".select-result").hide();

        }
    });

    $(".eliminateCriteria").on("click", function () {
        $("#selectA").remove();
        $("#selectB").remove();
        $("#selectC").remove();
        $(".select-all").addClass("selected").siblings().removeClass("selected");
        $(".eliminateCriteria").hide();
        $(".select-no").show();
        $(".select-result").hide();

    });


    var query = $("#clearFloat").attr("name");
    $("#sortOption li").each(function (i, e) {
        $(e).click(function () {
            switch ($(e).attr("id")) {
                case "defaultSort":
                    query = query.replace(new RegExp("sort:[Z-z]*;"), "");
                    $("#search_url").attr("value", query);
                    console.log(query)
                    $("form").submit();
                    break;
                case "salesFirst":
                    query.indexOf("sort") >= 0 ? query = query.replace("retail_price", "sell_volume") : {};
                    query.indexOf("sell_volume") >= 0 ? $("#search_url").attr("value", query) : $("#search_url").attr("value", query + "sort:sell_volume;");
                    $("form").submit();
                    break;
                case "priceFirst" :
                    query.indexOf("sort") >= 0 ? query = query.replace("sell_volume", "retail_price") : {};
                    query.indexOf("retail_price") >= 0 ? $("#search_url").attr("value", query) : $("#search_url").attr("value", query + "sort:retail_price;");
                    $("form").submit();
                    break;
            }
            /*排序方式点击样式*/
            $(e).addClass("first").siblings().removeClass("first");
        })
    })
    /*若无搜索结果，则禁用排序按钮*/
    if (parseInt($("#number").text()) < 1) {
        $("#sortOption li").unbind("click");
    }
    /*分页按钮*/
    $("#pageList .pageIndex").each(function (i, e) {
        $(e).click(function () {
            $(e).addClass("am-active").siblings().removeClass("am-active");
        })
    })
    $("#pageList .pageIndex a").click(function () {
        query = query.replace(new RegExp("page:[\\d]+"), "page:" + $(this).text());
        $("#search_url").attr("value", query);
        $("form").submit();
    })
    $("#nextButton").click(function () {
        if (!$(this).hasClass("am-disabled")) {
            var page = parseInt($("#pageList li.am-active a").text()) + 1;
            query = query.replace(new RegExp("page:[\\d]+"), "page:" + page);
            console.log("page:" + page)
            console.log("query:" + query)
            $("#search_url").attr("value", query);
            $("form").submit();
        }
    })
    $("#lastButton").click(function () {
        if (!$(this).hasClass("am-disabled")) {
            var page = parseInt($("#pageList li.am-active a").text()) - 1;
            query = query.replace(new RegExp("page:[\\d]+"), "page:" + page);
            console.log("page:" + page)
            console.log("query:" + query)
            $("#search_url").attr("value", query);
            $("form").submit();
        }
    })
})